<?php

declare(strict_types=1);

namespace App\Controller\playlet;
use App\Controller\BaseController;
use App\Model\Applet;
use App\Model\AppletPlayletRel;
use App\Model\DataSpread;
use App\Model\Playlet;
use App\Model\PlayletEpisodes;
use App\Service\queue\DramaService;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\Di\Annotation\Inject;
use App\Common\Response\SystemCode;
use App\Service\playlet\PlayletEpisodesService;

#[AutoController]
class PlayletEpisodesController  extends BaseController
{

    #[Inject]
    private PlayletEpisodesService $playletEpisodesService;

    #[Inject]
    private DramaService $dramaService;
    /**
     * 剧集列表
     */
    public function Index(RequestInterface $request)
    {

        $data = $this->playletEpisodesService->search($request->query());

        return $this->response(SystemCode::SYSTEM_SUCCESS, '请求成功',$data);


    }
    /**
     * 剧集详情
     */
    public function View(int $id,RequestInterface $request)
    {
        if(!empty($id)) {
            $playletEpisodesInfo = PlayletEpisodes::query()->select(['id','playlet_id','playlet_number','playlet_number_name','cover','amount','play_url','original_play_url','likes_number','share_number','created_at','updated_at'])->where('id', $id)->first();
            if (empty($playletEpisodesInfo)) {
                return $this->response(SystemCode::SYSTEM_ERROR, '短剧集数不存在');
            }
            return $this->response(SystemCode::SYSTEM_SUCCESS, '请求成功', $playletEpisodesInfo);
        }
    }
    /**
     * 剧集创建
     */
    public function Create(RequestInterface $request){

        $result = $this->playletEpisodesService->createOrUpdate($request->post());

        if($result['code'] == SystemCode::SYSTEM_SUCCESS){
            return $this->response(SystemCode::SYSTEM_SUCCESS, '创建成功');
        }else{
            return $this->response(SystemCode::SYSTEM_ERROR, $result['message']);

        }
    }
    /**
     * 剧集删除
     */
    public function Delete(int $id,RequestInterface $request)
    {
        if(!empty($id)) {
            $playletEpisodesInfo = PlayletEpisodes::query()->where('id', $id)->first();
            if (empty($playletEpisodesInfo)) {
                return $this->response(SystemCode::SYSTEM_ERROR, '短剧剧集不存在');
            }
            $playletEpisodesInfo->deleted_at = date('Y-m-d H:i:s');
            $playletEpisodesInfo->save();
            return $this->response(SystemCode::SYSTEM_SUCCESS, '操作成功');
        }
    }
    /**
     * 剧集更新
     */
    public function Update(int $id,RequestInterface $request){

        if(empty($id)){
            return $this->response(SystemCode::SYSTEM_ERROR, '错误请求');
        }else{

            $playletEpisodesInfo = PlayletEpisodes::query()->select(['id','playlet_id','playlet_number','playlet_number_name','cover','amount','play_url','original_play_url'])->where('id', $id)->first();

            if(empty($playletEpisodesInfo)){
                return $this->response(SystemCode::SYSTEM_ERROR, '短剧集数不存在');
            }
            if($request->getMethod() == 'GET'){
                return $this->response(SystemCode::SYSTEM_SUCCESS, '查询成功',$playletEpisodesInfo);
            }else{
                $result = $this->playletEpisodesService->createOrUpdate($request->post(),true,$playletEpisodesInfo);

                if($result['code'] == SystemCode::SYSTEM_SUCCESS){
                    return $this->response(SystemCode::SYSTEM_SUCCESS, '修改成功');
                }else{
                    return $this->response(SystemCode::SYSTEM_ERROR, $result['message']);

                }
            }
        }
    }

    public function test(){

//       $spreadData =  DataSpread::query()
//           ->whereNotNull('dy_parameter')->whereNotNull('dy_link')->get();
//
//       if(!empty($spreadData)){
//
//           foreach ($spreadData as $spreadDatum){
//
//               //查短剧
//              $playletInfo =  Playlet::query()->where('id', $spreadDatum->playlet_id)->select(['id','album_id'])->first();
//               var_dump($spreadDatum->playlet_id);
//              if(!empty($playletInfo)){
//                //查询短剧剧集
//                  $episodeData = PlayletEpisodes::query()->where('playlet_id',$playletInfo->id)
//                      ->where('playlet_number',$spreadDatum->playlet_number)
//                      ->select('dy_episodes_id')
//                      ->first();
//                  if(!empty($episodeData)){
//                      var_dump(123);
//                      $spreadDatum->dy_parameter = $spreadDatum->dy_parameter.'&tt_album_id='.$playletInfo->album_id.'&tt_episode_id='.$episodeData->dy_episodes_id;
//                      $spreadDatum->dy_link = $spreadDatum->dy_link.'&tt_album_id='.$playletInfo->album_id.'&tt_episode_id='.$episodeData->dy_episodes_id;
//                      $spreadDatum->save();
//                  }
//              }
//           }
//       }
    }


}